package com.epam.web_project.bank.command.show_commands;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.epam.web_project.bank.command.Command;
import com.epam.web_project.bank.dao.account_dao.AccountDAO;
import com.epam.web_project.bank.dao.factory.MySQLDAOFactory;
import com.epam.web_project.bank.db.ConnectionPool;
import com.epam.web_project.bank.entity.Account;
import com.epam.web_project.bank.util.AppPropertiesLoader;

public class ShowAccountForm implements Command {

	//show admin a page for adding iban, card number, etc 
	//for creating and approving the account
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String resPage;
		AppPropertiesLoader props = AppPropertiesLoader.getInstance();
		MySQLDAOFactory daoFactory = new MySQLDAOFactory();
		daoFactory.setConnectionPool(ConnectionPool.getInstance());
		AccountDAO accountDAO=daoFactory.getAccountDAO();
		int accId=-1;
		if(request.getParameter("accountId")!=null) {
			accId=Integer.valueOf(request.getParameter("accountId"));
		}
		Account account=accountDAO.findById(accId);
		if(account!=null) {
			request.setAttribute("account", account);
			resPage = props.getProperty("account_form_page");
		} else {
			resPage=props.getProperty("show_admin_main_command");
		}
		return resPage;
	}
	
	
}
